package com.study.hot.newActivity

import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

/**
 * <pre>
 *     author : slx
 *     time   : 2020/04/13
 *     desc   :
 * </pre>
 */
class GetResultActivity : AppCompatActivity() {
    private lateinit var photo: ImageView
    private lateinit var takePhotoObserver: TakePhotoObserver
    private lateinit var takePhotoLiveData: TakePhotoLivedata
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
       /* takePhotoObserver = TakePhotoObserver(activityResultRegistry) { bitmap ->
            photo.setImageBitmap(bitmap)
        }
        takePhotoLiveData = TakePhotoLivedata(activityResultRegistry)
        takePhotoLiveData.observeForever {
            photo.setImageBitmap(it)
        }*/
      /*  lifecycle.addObserver(takePhotoObserver)
        linearLayout {
            orientation = LinearLayout.VERTICAL
            photo = imageView {
            }
            button {
                text = "启动Activity获取结果"
                onClick {
                    startActivity.launch(Intent(this@GetResultActivity, ResultActivity::class.java))
                }
            }
            button {
                text = "打电话"
                onClick {
                    dial("123")
                }
            }
            button {
                text = "申请权限"
                onClick {
                    requestPermission.launch(Manifest.permission.READ_PHONE_STATE)
                }
            }
            button {
                text = "拍照"
                onClick {
                    takePicture()
                }
            }

            button {
                text = "拍照(Observer实现解耦)"
                onClick {
                    takePhotoObserver.takePhoto()
                }
            }*/

           /* button {
                text = "拍照(LiveData实现解耦)"
                onClick {
                    takePhotoLiveData.activityResultLauncher()
                }
            }*/
        }


  /*  //启动Activity获取结果
    //prepareCall 底层通过ActivityResultRegistry注册
    private val startActivity = prepareCall(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult? ->
        toast(result?.data?.getStringExtra("value") ?: "")
    }

    //打电话
    private val dial = prepareCall(ActivityResultContracts.Dial()) { result ->
        toast("dial $result")
    }

    //申请权限
    private val requestPermission = prepareCall(ActivityResultContracts.RequestPermission()) { result ->
        toast("requestPermission $result")
    }

    //拍照
    private val takePicture = prepareCall(ActivityResultContracts.TakePicture()) { result ->
        photo.setImageBitmap(result)
    }

    private val takePictureDrawable = prepareCall(TakePicDrawable()) { result ->
        toast("takePictureDrawable:$result")
    }

    //自定义返回值（ActivityResultContracts.TakePicture()默认返回bitmap，我们返回Drawable）
    private class TakePicDrawable : ActivityResultContract<Void, Drawable>() {
        override fun createIntent(input: Void?): Intent {
            return Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        }

        override fun parseResult(resultCode: Int, intent: Intent?): Drawable? {
            if (intent == null || resultCode != Activity.RESULT_OK) return null
            val bitmap = intent.getParcelableExtra<Bitmap>("data")
            return BitmapDrawable(bitmap)
        }

    }*/
}